#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QGridLayout>
#include <QtGui/QSpinBox>
#include <QtGui/QFontComboBox>
#include <QtGui/QRadioButton>
#include <QtGui/QCheckBox>
#include <QtGui/QPushButton>
#include <QtCore/QSettings>
#include "Const.h"
#include "ColorButton.h"
#include "TabsView.h"
#include "SettingsDialog.h"

SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent) {
	settings = new QSettings(ORGANIZATION,PROGRAM_NAME);
	
	tabsView = new TabsView(this);
	
	sessionTab = new QWidget;
	saveSessionWhenCloseCheckBox = new QCheckBox(tr("Save session when close"));
	whenTheProgramStartComboBox = new QComboBox;
	whenTheProgramStartComboBox->addItem(tr("Show session dialog"));
	whenTheProgramStartComboBox->addItem(tr("Open last opened session"));
	whenTheProgramStartComboBox->addItem(tr("Start new session"));
	
	editionTab = new QWidget;
	fontComboBox = new QFontComboBox;
	fontSizeSpinBox = new QSpinBox;
	
	tabSizeSpinBox = new QSpinBox;
	replaceTabsWithSpacesCheckBox = new QCheckBox(tr("Replace tabs by spaces"));
	showIndentsCheckBox = new QCheckBox(tr("Show indents"));
	
	viewTab = new QWidget;
	showLinesNumbersCheckBox = new QCheckBox(tr("Show lines numbers"));
	adjustTextCheckBox = new QCheckBox(tr("Adjust text by width"));
	highlightLineCheckBox = new QCheckBox(tr("Highlight current line"));
	
	currentLineColorButton = new ColorButton(QColor(0,0,0),parent);
	currentLineColorButton->setEnabled(false);
	connect(highlightLineCheckBox,SIGNAL(toggled(bool)),currentLineColorButton,SLOT(setEnabled(bool)));
	
	autoCompletionTab = new QWidget;
	replaceWordsCheckBox = new QCheckBox(tr("Replace words"));
	autoCompletionSensSpinBox = new QSpinBox;
	autoCompletionSensSpinBox->setMaximum(10);
	autoCompletionSensSpinBox->setMinimum(1);
	useAutoCompletionCheckBox = new QCheckBox(tr("Use AutoCompletion"));

	QGridLayout *sessionTabLayout = new QGridLayout;
	sessionTabLayout->addWidget(new QLabel(tr("When the program start") + ":"),0,0);
	sessionTabLayout->addWidget(whenTheProgramStartComboBox,0,1);
	sessionTabLayout->addWidget(saveSessionWhenCloseCheckBox,1,0,1,2);
	sessionTabLayout->setRowStretch(3,1);
	sessionTab->setLayout(sessionTabLayout);
	
	QGridLayout *editionTabLayout = new QGridLayout;
	editionTabLayout->addWidget(new QLabel(tr("Font")),0,0);
	editionTabLayout->addWidget(fontComboBox,0,1);
	editionTabLayout->addWidget(new QLabel(tr("Size")),1,0);
	editionTabLayout->addWidget(fontSizeSpinBox,1,1);
	editionTabLayout->addWidget(new QLabel(tr("Tab size")),2,0);
	editionTabLayout->addWidget(tabSizeSpinBox,2,1);
	editionTabLayout->addWidget(replaceTabsWithSpacesCheckBox,3,0,1,2);
	editionTabLayout->addWidget(showIndentsCheckBox,4,0,1,2);
	editionTabLayout->setRowStretch(5,1);
	editionTab->setLayout(editionTabLayout);
	
	QHBoxLayout *highlightCurrentLineLayout = new QHBoxLayout;
	highlightCurrentLineLayout->addWidget(highlightLineCheckBox);
	highlightCurrentLineLayout->addWidget(currentLineColorButton);
	
	QVBoxLayout *viewTabLayout = new QVBoxLayout;
	viewTabLayout->addWidget(showLinesNumbersCheckBox);
	viewTabLayout->addWidget(adjustTextCheckBox);
	viewTabLayout->addLayout(highlightCurrentLineLayout);
	viewTabLayout->addStretch();
	viewTab->setLayout(viewTabLayout);
	
	QGridLayout *autoCompletionTabLayout = new QGridLayout;
	autoCompletionTabLayout->addWidget(useAutoCompletionCheckBox,0,0,1,2);
	autoCompletionTabLayout->addWidget(new QLabel(tr("AutoCompletion sensivity") + ":"),1,0);
	autoCompletionTabLayout->addWidget(autoCompletionSensSpinBox,1,1);
	autoCompletionTabLayout->addWidget(replaceWordsCheckBox,2,0,1,2);
	autoCompletionTabLayout->setRowStretch(4,1);
	autoCompletionTab->setLayout(autoCompletionTabLayout);

	tabsView->addTab(sessionTab,QIcon(":/icons/sessions_manager.png"),tr("Sessions"));
	tabsView->addTab(editionTab,QIcon(":/icons/edition.png"),tr("Edition"));
	tabsView->addTab(viewTab,QIcon(":/icons/view.png"),tr("View"));
	tabsView->addTab(autoCompletionTab,QIcon(":/icons/autocompletion.png"),tr("AutoCompletion"));
	tabsView->addTab(new QWidget,QIcon(":/icons/plugins.png"),tr("Plugins"));
	
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->setMargin(0);
	mainLayout->addWidget(tabsView);
	setLayout(mainLayout);
	
	setWindowTitle(tr("Settings"));
	setMinimumWidth(400);
	resize(400,200);
	loadSettings();
	
	// Connections
	connect(tabsView,SIGNAL(tabChanged(QWidget*)),this,SLOT(tabChanged(QWidget*)));
	
	connect(whenTheProgramStartComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(setWhenTheProgramStartAction(int)));
	connect(saveSessionWhenCloseCheckBox,SIGNAL(toggled(bool)),this,SLOT(setSaveSessionWhenClose(bool)));
	connect(fontComboBox,SIGNAL(currentFontChanged(const QFont&)),this,SLOT(setCurrentFont(const QFont&)));
	connect(fontSizeSpinBox,SIGNAL(valueChanged(int)),this,SLOT(setCurrentFontSize(int)));
	connect(tabSizeSpinBox,SIGNAL(valueChanged(int)),this,SLOT(setTabSize(int)));
	connect(replaceTabsWithSpacesCheckBox,SIGNAL(toggled(bool)),this,SLOT(setReplaceTabsWithSpaces(bool)));
	connect(showIndentsCheckBox,SIGNAL(toggled(bool)),this,SLOT(setShowIndents(bool)));
	connect(showLinesNumbersCheckBox,SIGNAL(toggled(bool)),this,SLOT(setShowLinesNumbers(bool)));
	connect(adjustTextCheckBox,SIGNAL(toggled(bool)),this,SLOT(setAdjustText(bool)));
	connect(highlightLineCheckBox,SIGNAL(toggled(bool)),this,SLOT(setHighlightLine(bool)));
	connect(currentLineColorButton,SIGNAL(clicked()),this,SLOT(setCurrentLineColor()));
	connect(autoCompletionSensSpinBox,SIGNAL(valueChanged(int)),this,SLOT(setAutoComplSensivity(int)));
	connect(replaceWordsCheckBox,SIGNAL(toggled(bool)),this,SLOT(setAutoComplReplaceWords(bool)));
	connect(useAutoCompletionCheckBox,SIGNAL(toggled(bool)),this,SLOT(setUseAutoCompletion(bool)));
	
	tabsView->setCurrentWidget(sessionTab);
}

SettingsDialog::~SettingsDialog() {
	delete whenTheProgramStartComboBox;
	delete saveSessionWhenCloseCheckBox;
	delete sessionTab;
	
	delete fontComboBox;
	delete fontSizeSpinBox;
	delete tabSizeSpinBox;
	delete replaceTabsWithSpacesCheckBox;
	delete showIndentsCheckBox;
	delete editionTab;
	
	delete showLinesNumbersCheckBox;
	delete adjustTextCheckBox;
	delete highlightLineCheckBox;
	delete currentLineColorButton;
	delete viewTab;
	
	delete replaceWordsCheckBox;
	delete autoCompletionSensSpinBox;
	delete useAutoCompletionCheckBox;
	delete autoCompletionTab;
	
	delete tabsView;
	
	delete settings;
}

void SettingsDialog::loadSettings() {
	whenTheProgramStartComboBox->setCurrentIndex(settings->value("Settings/WhenTheProgramStartAction",0).toInt());
	saveSessionWhenCloseCheckBox->setChecked(settings->value("Settings/SaveSessionWhenClose",true).toBool());
	fontComboBox->setCurrentFont(QFont(settings->value("Settings/Font","DejaVu Sans Mono").toString()));
	fontSizeSpinBox->setValue(settings->value("Settings/FontSize",10).toInt());
	tabSizeSpinBox->setValue(settings->value("Settings/TabSize",4).toInt());
	replaceTabsWithSpacesCheckBox->setChecked(settings->value("Settings/ReplaceTabsBySpaces",false).toBool());
	showIndentsCheckBox->setChecked(settings->value("Settings/ShowIndents",true).toBool());
	showLinesNumbersCheckBox->setChecked(settings->value("Settings/ShowLinesNumbers",true).toBool());
	adjustTextCheckBox->setChecked(settings->value("Settings/AdjustTextByWidth",true).toBool());
	highlightLineCheckBox->setChecked(settings->value("Settings/HighlightCurrentLine",true).toBool());
	currentLineColorButton->setButtonColor(settings->value("Settings/CurentLineColor",QColor(225,250,255)).value<QColor>());
	autoCompletionSensSpinBox->setValue(settings->value("Settings/AutoComplSensivity",2).toInt());
	replaceWordsCheckBox->setChecked(settings->value("Settings/AutoComplReplaceWords",true).toBool());
	useAutoCompletionCheckBox->setChecked(settings->value("Settings/UseAutoCompletion",false).toBool());
}

void SettingsDialog::setWhenTheProgramStartAction(int index) {
	settings->setValue("Settings/WhenTheProgramStartAction",index);
}

void SettingsDialog::setSaveSessionWhenClose(bool save) {
	settings->setValue("Settings/SaveSessionWhenClose",save);
	emit (settingsChanged());
}

void SettingsDialog::setCurrentFont(const QFont& font) {
	settings->setValue("Settings/Font",font.family());
	emit (settingsChanged());
}

void SettingsDialog::setCurrentFontSize(int size) {
	settings->setValue("Settings/FontSize",size);
	emit (settingsChanged());
}

void SettingsDialog::setTabSize(int size) {
	settings->setValue("Settings/TabSize",size);
	emit (settingsChanged());
}

void SettingsDialog::setReplaceTabsWithSpaces(bool toReplace) {
	settings->setValue("Settings/ReplaceTabsBySpaces",toReplace);
	emit (settingsChanged());
}

void SettingsDialog::setShowIndents(bool show) {
	settings->setValue("Settings/ShowIndents",show);
	emit (settingsChanged());
}

void SettingsDialog::setShowLinesNumbers(bool show) {
	settings->setValue("Settings/ShowLinesNumbers",show);
	emit (settingsChanged());
}

void SettingsDialog::setAdjustText(bool adjust) {
	settings->setValue("Settings/AdjustTextByWidth",adjust);
	emit (settingsChanged());
}

void SettingsDialog::setHighlightLine(bool highlight) {
	settings->setValue("Settings/HighlightCurrentLine",highlight);
	emit (settingsChanged());
}

void SettingsDialog::setCurrentLineColor() {
	settings->setValue("Settings/CurentLineColor",currentLineColorButton->color());
	emit (settingsChanged());
}

void SettingsDialog::setAutoComplSensivity(int sensivity) {
	settings->setValue("Settings/AutoComplSensivity",sensivity);
	if (useAutoCompletionCheckBox->isChecked())
		emit (settingsChanged());
}

void SettingsDialog::setAutoComplReplaceWords(bool replace) {
	settings->setValue("Settings/AutoComplReplaceWords",replace);
	if (useAutoCompletionCheckBox->isChecked())
		emit (settingsChanged());
}

void SettingsDialog::setUseAutoCompletion(bool use) {
	settings->setValue("Settings/UseAutoCompletion",use);
	emit (settingsChanged());
}

void SettingsDialog::tabChanged(QWidget *currentWidget) {
	setMaximumHeight(currentWidget->sizeHint().height());
	
	if (currentWidget == sessionTab)
		setWindowTitle(tr("Sessions") + " - " + tr("Settings"));
	else if (currentWidget == editionTab)
		setWindowTitle(tr("Edition") + " - " + tr("Settings"));
	else if (currentWidget == viewTab)
		setWindowTitle(tr("View") + " - " + tr("Settings"));
	else if (currentWidget == autoCompletionTab)
		setWindowTitle(tr("AutoCompletion") + " - " + tr("Settings"));
}
